const Router = require('koa-router');
// 校验、加密等中间件
const { userValidator, userNameRepeat, cryptPassword, verifyLogin} = require('../middleware/user.middleware');
const { tokenValidator } = require('../middleware/auth.middleware');
const { idValidator } = require('../middleware/validate.middleware');
// 业务路由拆分 -- controller层
const {register, login, updatePassword, getUserList, updateUser} = require('../controller/user.controller');

// 统一前缀
const router = new Router({prefix: '/user'});

// 注册 在执行register前 先执行userValidator中间件对参数进行非空校验 userNameRepeat用户名重复校验 cryptPassword中间件进行加密
router.post('/register', userValidator, userNameRepeat, cryptPassword, register);
router.post('/login', userValidator, verifyLogin, login);

router.get('/list', tokenValidator, getUserList);
router.post('/update', tokenValidator, idValidator, updateUser);

router.post('/update/password', tokenValidator, cryptPassword, updatePassword);

module.exports = router;